package com.gluxen.service;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import com.gluxen.common.constant.SystemConstant;
import com.gluxen.common.exception.BusinessException;
//import com.gluxen.common.logaop.MyLog;
import com.gluxen.dao.admin.IUserDao;
import com.gluxen.entity.User;
import com.gluxen.service.IUserService;
import com.gluxen.util.Ufn;

@Service
public class UserService extends BaseService implements IUserService{
	@Resource
	IUserDao userDao;

	@Override
	public boolean validateNickName(String nickName) {
		return this.userDao.validateNickName(nickName);
	}
	@Override
	public boolean validateOpenID(String openID) {
		return this.userDao.validateOpenID(openID);
	}

	//@MyLog(operationName = "添加用户及其相关联的角色权限", operationType = "add")
	@Override
	public void addUser(User user) {
		add(user);
		System.out.println("添加的用户：nickName:"+user.getNickname()+" :job :"+user.getJob());
	}

	//@MyLog(operationName = "user登陆", operationType = "login")
	@SuppressWarnings("unchecked")
	@Override
	public User queryUserForLogin(String nickname, String password, HttpSession session) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		System.out.println("loginUser:"+nickname+":"+password);
		paramMap.put("nickname", nickname);
		User user = (User)queryUnqueResult("from User uu where uu.isDelete = 0 and "
						+ "uu.nickname = :nickname", paramMap);
		if (user == null)
			throw new BusinessException("用户不存在");
		else if (!user.getPassword().equals(password))
			throw new BusinessException("密码错误");
		
		List<Integer> roleIdList = (List<Integer>) queryListByNavtiveSql("select FK_ROLE_ID from USER_ROLE where FK_USER_ID=" + user.getId());
		if(roleIdList == null || roleIdList.size() == 0)
			throw new BusinessException("该用户未分配角色，暂时不能访问本系统！");
		user.setRoles(new HashSet<Integer>(roleIdList));
		List<Integer> privList = (List<Integer>) queryListByNavtiveSql("select PRIV_ID from ROLE_PRIV where FK_ROLE_ID in (" + Ufn.join(roleIdList.toArray()) + ")");
		Set<Integer> privs = new HashSet<Integer>(privList);
		privs.add(0);
		user.setPrivs(privs);
		
		session.setAttribute(SystemConstant.CURRENT_USER,user);
		//session.setAttribute("OPenID", "");
		return user;
	}

	//切面处理，得到日志信息
	//@MyLog(operationName = "批量删除员工", operationType = "delete")
	@Override
	public void deleteUsers(String[] ids) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < ids.length - 1; i++)
			sb.append(ids[i] + ",");
		sb.append(ids[ids.length - 1]);
		//updateByHql("delete from UserRole ur where ur.user.id in (" + sb.toString() + ")");
		delete(User.class, sb.toString());
	}

	//@MyLog(operationName = "更新", operationType = "update")
	@Override
	public void updateUser(User user) {
		update(user);
	}

	@Override
	public User queryByLoginName(String loginName) throws Exception {
		String hql = "from User o where o.isDelete=0 and o.loginName = '"+loginName+"'";
		return (User) userDao.getUnqueResult(hql, null);
	}

	//@MyLog(operationName = "修改密码", operationType = "update")
	@Override
	public void updatePassword(User user, String password) throws Exception {
		user.setPassword(password);
		update(user);
	}

	@Override
	public User queryUserByID(Integer u_id) {
		String hql = "from User o where o.isDelete=0 and o.id = '"+u_id+"'";
		return (User)userDao.getUnqueResult(hql, null);
	}
}
